<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* .animation{
            animation: move 2s linear infinite alternate;
            width: 20px;
            height: 20px;
            border: 1px solid red;
            animation-play-state: paused;
        }
        @keyframes move {
            0% {
                transform: translateX(0);
            }

            100% {
                transform: translateX(200px);
            }
        }

        .stop:hover ~ .animation{
            animation-play-state: paused;
        }

        .play:hover ~ .animation{
            animation-play-state: running;
        } */


        .animation {
            width: 10px;
            height: 10px;
            border: 1px solid red;
            animation: move 2s linear infinite alternate;
        }

        @keyframes move {
            0% {
                transform: translate(0);
            }

            100% {
                transform: translate(200px);
            }
        }

        .btn {
            width: 50px;
            text-align: center;
            border: 1px solid #ddd;
            padding: 10px;
            border-radius: 5px;
            cursor: pointer;
        }

        .btn:hover {
            background: #ddd;
            color: #333;
        }

        .btn:active {
            background: #aaa;
        }
    </style>
</head>

<body>
    <!-- css实现点击动画实现暂停与播放 -->
    <!-- <div class="box">
        <div class="btn stop">stop</div>
        <div class="btn play">play</div>
        <div class="animation"></div>

    </div> -->


    <!-- <div class="box">
        <div class="btn stop">stop</div>
        <div class="btn play">play</div>
        <div class="animation"></div>

    </div>
    <div class="animation"></div> -->


    <div class="btn stop">stop</div>
    <div class="btn play">play</div>
    <div class="animation"></div>

    <script>
        document.querySelector('.btn').addEventListener('click', function () {
            let btn = document.querySelector('.btn');
            let elem = document.querySelector('.animation');
            let state = elem.style['animationPlayState'];
            if (state === 'paused') {
                elem.style['animationPlayState'] = 'running';
                btn.innerText = 'stop';
            } else {
                elem.style['animationPlayState'] = 'paused';
                btn.innerText = 'play';
            }
        });
    </script>
</body>

</html>